from google.appengine.ext import db
from misc import ImageContainer
from equipment import EquipmentContainer
from users import User
from gmaps import encode_pairs
from regions import Region
#from waypoints import WayPoint

class Journey(EquipmentContainer):
#class Journey(db.Model):
    
    added       = db.DateTimeProperty(auto_now_add=True)
    modified    = db.DateTimeProperty(auto_now=True)
    
    name        = db.StringProperty()
    desc        = db.TextProperty()
    
    started		= db.DateProperty()
    ended		= db.DateProperty()
    
    length      = db.StringProperty()
    
    user        = db.UserProperty(required=True,auto_current_user_add=True)
    
    public      = db.BooleanProperty()
    
    rating      = db.RatingProperty()
    """ reference to jouney profile, like a camino way, something else, 
    should make it easy to search for existing waypoints """
    profile     = db.ReferenceProperty()

    last_wp     = db.IntegerProperty()
    region		= db.ReferenceProperty( Region, collection_name="region_journeys" )

    @property
    def waypoints(self):
        return db.GqlQuery( "select * from WayPoint where journey = :1 order by journey, pos", self ).fetch(1000)
        
        #return self.journey_waypoints
    @property
    def waypoints_encoded(self):
        
        pts = []
        for wp in self.waypoints:
            pts.append([ float(wp.lat()), float(wp.lng()) ])
        
        return encode_pairs( pts )
        
    def add_waypoint( self, wp ):
        
        wp.jouney = self
        wp.put()
        
        
#    def __init__(self,something):
#        
#        self.name = "efef"
#        self.desc = ""
#        self.public = False
#        self.rating = 0
#    
#        EquipmentContainer.__init__(self,something)
   
    
